from pymongo import *
from datetime import *
import re

client = MongoClient("localhost", 27017)
note = client.mydb
mySet = note.myset
def noteFunc():
    strData = input("请输入录入的信息\n")
    strTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    writeDB(strTime,strData)
#name:{$regex:/joe/}
def findFunc():
    flag = input("请选择，时间查询输1，内容查询输入2\n")
    data = input("请输入关键字\n")
    if "1" == flag:
        tem = '/%s.*/'%data
        strData = {'time':tem}
        #strData = {'time':{'$regex':tem}}
        #strData = {'time':{'$regex':re.compile(data)}}
    elif "2" == flag:
        tem = '/%s/'%data
        strData = {'content':tem}
        #strData = {'content': {'$regex': re.compile(data)}}
    else:
        print("输入错误")
    query = mySet.find(strData,{'time':1,'content':1,'_id':0})
    # db.myset.find({time:{$regex:/2018/}})
    for data in query:
        print(data)
def writeDB(sTime,sData):
    post = {'time': sTime, 'content': sData}
    mySet.insert(post)
def readDB():
    query = mySet.find({},{'time':1,'content':1,'_id':0})
    print("数据库信息：")
    for data in query:
        print(data)
def init():
    pass

def main():
    readDB()
    while True:
        choose = input("选择，输入note记录信息，输入find查询信息，输入exit退出\n")
        if "note" == choose:
            noteFunc()
            readDB()
        elif "find" == choose:
            findFunc()
        elif "exit" == choose:
            break
        else:
            print("输入错误，请重新输入")

#if __name__ == '__main__':
main()